import React, {useEffect, useState} from 'react';
import {message, PageHeader, Spin} from 'antd';
import TweetsList from "../indexMain/tweetsList"
import style from"./index.module.css"
import {get} from "../../utils/get";


const SearchList=(props)=> {
    const [searchData,setData]=useState([])
    const [loading,setLoading]=useState(false)
    const {keyWord} = props.location.state
    function workerTest(total,data,keyWord) {
        console.log('%c 开始多线程测试 ', 'color:#fff; background:#00897b ')
        const workerList = []
        let number=total/30
        for (let i = 0; i < number; i++) {
            const workerItem = new Promise((resolve, reject) => {
                const myWorker = new Worker(new URL('worker.js',import.meta.url))
                myWorker.postMessage({
                    function: 'search',
                    data:data.slice(i*30,(i+1)*30),
                    keyWord,
                })
                myWorker.onmessage = (e) => {
                    resolve(e.data)
                    // 关闭worker线程
                    myWorker.terminate()
                }
            })
            workerList.push(workerItem)
        }
        let dataArr =[]
        Promise.all(workerList).then(res => {
            res.map(item=>{
                dataArr.push(...item.data)
            })
            setData(dataArr)
            console.timeEnd('worker多线程执行时间')
        })
    }
    useEffect(async ()=>{
        setLoading(true);
        // const dataMsg=[]
        const res=await get(`/share/list?page=${1}&limit=1`)
        let total =res.data.page.totalCount
        if(res.data.code===0)  {
            const res2 = await get(`/share/list?page=${1}&limit=${total}`)
            console.time('worker多线程执行时间')
            await workerTest(total,res2.data.page.list,keyWord)
            setLoading(false)
         } 
        // if(res.data.code===0)
        // {
        //     let data = res.data.page.list;
        //     const search =data.filter(item=>{
        //         return item.content.includes(keyWord)
        //     })
        //     setData(search);
        // }
        // else message.error('网络错误')
        // setLoading(false);
    },[props])
    
    return (
        <div>
            <PageHeader
                className={style.header}
                onBack={() =>{props.history.goBack()}}
                title={`搜索结果`}
            />
            <Spin tip={"在搜了！在搜了！不要催我..."} spinning={loading}>
            <TweetsList type={3} data={searchData} searchTotal={searchData.length}/>
            </Spin>
        </div>
    );
}

export default SearchList;
